package com.example.exceltest;

import android.util.Log;

//import com.lowagie.text.Cell;
//import com.lowagie.text.Document;
//import com.lowagie.text.Element;
//import com.lowagie.text.Font;
//import com.lowagie.text.PageSize;
//import com.lowagie.text.Phrase;
//import com.lowagie.text.Table;
//import com.lowagie.text.pdf.BaseFont;
//import com.lowagie.text.pdf.PdfPCell;
//import com.lowagie.text.pdf.PdfPTable;
//import com.lowagie.text.pdf.PdfWriter;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
//import com.sun.java_cup.internal.internal_error;

public class CreatePdf {
    Document document = new Document();// 建立一个Document对象

    private static Font headfont ;// 设置字体大小
    private static Font keyfont;// 设置字体大小
    private static Font textfont;// 设置字体大小



    static{
        BaseFont bfChinese;
        try {
            //bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
            keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
            textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public CreatePdf(File file) {
        document.setPageSize(PageSize.A4);// 设置页面大小
        try {
            PdfWriter.getInstance(document,new FileOutputStream(file));
            document.open();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    int maxWidth = 520;


    public PdfPCell createCell(String value, Font font, int align){
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value,font));
        return cell;
    }

    public PdfPCell createCell(String value, Font font){
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value,font));
        return cell;
    }

    public PdfPCell createCell(String value, Font font, int align, int colspan){
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value,font));
        return cell;
    }
    public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag){
        PdfPCell cell = new PdfPCell();
        Log.e("PdfPCell===", "==="+document);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value,font));
        cell.setPadding(3.0f);

        Log.e("PdfPCell===1", "==="+document);

        if(!boderFlag){
            cell.setBorder(0);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);
        }

        Log.e("PdfPCell===2", "==="+document);

        return cell;
    }
    public PdfPTable createTable(int colNumber){
        Log.e("createTable===", "==="+document);
        PdfPTable table = new PdfPTable(colNumber);
        try{
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        }catch(Exception e){
            Log.e("createTable===e", "==="+e);
            e.printStackTrace();
        }
        return table;
    }
    public PdfPTable createTable(float[] widths){
        PdfPTable table = new PdfPTable(widths);
        try{
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        }catch(Exception e){
            e.printStackTrace();
        }
        return table;
    }

    public PdfPTable createBlankTable(){
        PdfPTable table = new PdfPTable(1);
        table.getDefaultCell().setBorder(0);
        table.addCell(createCell("", keyfont));
        table.setSpacingAfter(20.0f);
        table.setSpacingBefore(20.0f);
        return table;
    }

    public void generatePDF() throws Exception {
        try{

//            BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//            headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
//            keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
//            textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小

            Log.e("generatePDF===", keyfont+"==="+document);

            PdfPTable table = createTable(4);

            Log.e("generatePDF===1", keyfont+"==="+document);

            PdfPCell cell = createCell("学生信息列表：", keyfont, Element.ALIGN_LEFT,4,false);

            Log.e("generatePDF===2", cell+"==="+document);

            table.addCell(cell);


            PdfPCell cell2 = createCell("姓名", keyfont, Element.ALIGN_CENTER);
//            table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));
//            table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER));

            cell2.setColspan(2);
            table.addCell(cell2);

            table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER));

            for(int i=0;i<5;i++){

                if(i==0){
                    PdfPCell cell3 = createCell("姓名"+i, textfont);
                    cell3.setRowspan(2);
                    table.addCell(cell3);
                }else if(i>=2){
                    table.addCell(createCell("姓名"+i, textfont));
                }


                table.addCell(createCell(i+15+"", textfont));



                table.addCell(createCell((i%2==0)?"男":"女", textfont));
                table.addCell(createCell("地址"+i, textfont));
            }

//            Table table = new Table(10, 2);
//            int width[] = { 50, 50,20,40, 80, 50, 50, 50, 50, 60 };
//            table.setWidths(width);
//            table.setWidth(100); // 占页面宽度 %
//            table.setPadding(1);
//            table.setSpacing(0);
//            table.setBorderWidth(1);
//
////            Cell c1 = new Cell(new Phrase("应用", PdfUtils.GRAYFONT_CHINESE));
//            Cell c1 = new Cell(new Phrase("应用", keyfont));
//            c1.setRowspan(2);
////            setAlignCenter(c1);
//            table.addCell(c1);
//
//            c1 = new Cell(new Phrase("指标", keyfont));
//            c1.setColspan(3);
////            setAlignCenter(c1);
//            table.addCell(c1);

            Log.e("generatePDF===3", table+"==="+document);

            document.add(table);

            document.close();
        }catch(Exception e){
            Log.e("generatePDF===e", "==="+e);
            e.printStackTrace();
        }


    }

    public static void main(String[] args) throws Exception {
        System.out.println("begin");
        File file = new File("e:\\text.pdf");
        file.createNewFile();
        new CreatePdf(file).generatePDF();
        System.out.println("end");
    }


}
